package com.xiaobao.campusLostFoundAfter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.mapper.BrowsingHistoryMapper;
import com.xiaobao.campusLostFoundAfter.pojo.BrowsingHistory;
import com.xiaobao.campusLostFoundAfter.service.BrowsingHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {
    private static final Logger log = LoggerFactory.getLogger(BrowsingHistoryServiceImpl.class);
    private final BrowsingHistoryMapper browsingHistoryMapper;

    public BrowsingHistoryServiceImpl(BrowsingHistoryMapper browsingHistoryMapper) {
        this.browsingHistoryMapper = browsingHistoryMapper;
    }

    /**
     * 添加浏览历史记录
     * <p>
     * 此方法首先检查浏览记录是否已存在如果存在，则更新记录的创建时间如果不存在，则插入新的浏览记录
     *
     * @param browsingHistory 浏览历史对象，包含用户浏览的项目信息
     * @return 返回操作结果，包括成功或错误信息
     */
    @Override
    public Result<String> addBrowsingHistory(BrowsingHistory browsingHistory) {
        //获取当前详细时间
        browsingHistory.setCreateTime(new java.sql.Date(System.currentTimeMillis()));

        //初始化结果变量
        int result = 0;
        //做一个判断如果记录存在则更改时间
        if (Objects.equals(browsingHistory.getItemType(), "found")) {
            //判断这个类型的是否存在
            BrowsingHistory foundBrowsingHistory = browsingHistoryMapper.getFoundBrowsingHistory(browsingHistory.getItemId(), browsingHistory.getItemType(), browsingHistory.getUserId());
            if (foundBrowsingHistory != null) {
                //跟新浏览记录时间
                browsingHistoryMapper.updateBrowsingHistory(foundBrowsingHistory.getBhId(), new Date());
                return Result.success();
            } else {
                //设置创建时间并添加浏览记录
                Date createTime = new Date();
                browsingHistory.setCreateTime(createTime);
                result = browsingHistoryMapper.addBrowsingHistory(browsingHistory);
            }

        } else if (Objects.equals(browsingHistory.getItemType(), "lost")) {
            BrowsingHistory lostBrowsingHistory = browsingHistoryMapper.getLostBrowsingHistory(browsingHistory.getItemId(), browsingHistory.getItemType(), browsingHistory.getUserId());
            if (lostBrowsingHistory != null) {
                //跟新浏览记录时间
                log.info("更新浏览记录时间: {}", browsingHistory);
                browsingHistoryMapper.updateBrowsingHistory(lostBrowsingHistory.getBhId(), new Date());
                return Result.success();
            } else {
                //设置创建时间并添加浏览记录
                Date createTime = new Date();
                browsingHistory.setCreateTime(createTime);
                result = browsingHistoryMapper.addBrowsingHistory(browsingHistory);
            }
        }
        //根据结果返回成功或错误信息
        return result > 0 ? Result.success() : Result.error("添加浏览记录失败");
    }

    //删除浏览记录
    @Override
    public Result<String> deleteBrowsingHistory(Integer bhId) {
        int result = browsingHistoryMapper.deleteBrowsingHistory(bhId);
        return result > 0 ? Result.success() : Result.error("删除浏览记录失败");
    }

    //获取用户浏览的Found
    @Override
    public PageInfo<BrowsingHistory> getBrowsingHistoryOfFound(String userId, int page, int size) {
        String type = "found";
        PageHelper.startPage(page, size);
        List<BrowsingHistory> browsingHistories = browsingHistoryMapper.getBrowsingHistoryOfFound(userId, type);
        return new PageInfo<>(browsingHistories);
    }

    //获取用户浏览的失物
    @Override
    public PageInfo<BrowsingHistory> getBrowsingHistoryOfLost(String userId, int page, int size) {
        String type = "lost";
        PageHelper.startPage(page, size);
        List<BrowsingHistory> browsingHistories = browsingHistoryMapper.getBrowsingHistoryOfLost(userId, type);
        return new PageInfo<>(browsingHistories);
    }

    //更新浏览记录
    @Override
    public Result<String> updateBrowsingHistory(Integer bhId) {
        //获取当前时间
        Date updateTime = new Date();
        //根据bhId更新当前时间
        int result = browsingHistoryMapper.updateBrowsingHistory(bhId, updateTime);
        return result > 0 ? Result.success() : Result.error("更新浏览记录失败");
    }

}
